package com.han.fu.api.business.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.han.fu.api.business.entity.Activity;
import com.han.fu.api.business.mapper.ActivityMapper;
import com.han.fu.api.business.model.ao.ActivityAddAo;
import com.han.fu.api.business.model.ao.ActivityDeleteAo;
import com.han.fu.api.business.model.ao.ActivityPageAo;
import com.han.fu.api.business.model.ao.ActivityUpdateAo;
import com.han.fu.api.business.model.vo.ActivityInfoVo;
import com.han.fu.api.business.model.vo.ActivityVo;
import com.han.fu.api.business.service.IActivityService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动接口实现类
 *
 * @author Star.zhu
 * @since 2020-03-26
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements IActivityService {

    /**
     * 活动分页查询
     *
     * @param ao 查询条件
     * @return Page<ActivityVo> 分页数据
     */
    @Override
    public Page<ActivityVo> queryActivityPage(ActivityPageAo ao) {
        return baseMapper.queryActivityPage(ao);
    }

    /**
     * 活动明细查询
     *
     * @param id 明细ID
     * @return
     */
    @Override
    public ActivityInfoVo queryActivityDetail(String id) {
        ActivityInfoVo activityInfoVo = new ActivityInfoVo();
        Activity activity = super.getById(id);
        if (null != activity) {
            BeanUtils.copyProperties(activity, activityInfoVo);
        }
        return activityInfoVo;
    }

    /**
     * 活动新增数据
     *
     * @param ao 数据
     */
    @Override
    public void addActivity(ActivityAddAo ao) {
        Activity entity = new Activity();
        BeanUtils.copyProperties(ao, entity);
        super.save(entity);
    }

    /**
     * 活动修改数据
     *
     * @param ao 数据
     */
    @Override
    public void updateActivity(ActivityUpdateAo ao) {
        Activity entity = new Activity();
        BeanUtils.copyProperties(ao, entity);
        super.updateById(entity);
    }

    /**
     * 活动删除数据
     *
     * @param ao 数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivity(ActivityDeleteAo ao) {
        for (String id : ao.getIds()) {
            super.removeById(id);
        }
    }

}